package com.example;

import com.example.entity.*;
import com.example.repository.DriverRepository;
import com.example.repository.UserRepository;
import com.example.repository.VehicleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class DataLoader implements CommandLineRunner {
    private final VehicleRepository vehicleRepository;
    private final UserRepository userRepository;
    private final DriverRepository driverRepository;

    @Autowired
    public DataLoader(VehicleRepository vehicleRepository, UserRepository userRepository,DriverRepository driverRepository) {
        this.vehicleRepository = vehicleRepository;
        this.userRepository = userRepository;
        this.driverRepository = driverRepository;
    }

    @Override
    // 初始化测试数据包括一辆车和管理员身份用户
    public void run(String... args) throws Exception {
        if (vehicleRepository.count() == 0) {
            // 初始化测试车辆数据 - 高鹏
            Vehicle vehicle = new Vehicle();
            vehicle.setLicensePlate("鄂 K298L4");
            vehicle.setModel("东风标致 408");
            vehicle.setSeats(4);
            vehicle.setType(VehicleType.ECONOMY);
            vehicle.setStatus(VehicleStatus.AVAILABLE);
            vehicleRepository.save(vehicle);
            Driver driver = new Driver("高鹏","421301891267",vehicle);
            driverRepository.save(driver);
            // 初始化测试车辆数据 - 郭亮
            Vehicle vehicle_2 = new Vehicle();
            vehicle_2.setLicensePlate("鄂 J175K9");
            vehicle_2.setModel("北京现代伊兰特");
            vehicle_2.setSeats(5);
            vehicle_2.setType(VehicleType.LUXURY);
            vehicle_2.setStatus(VehicleStatus.AVAILABLE);
            vehicleRepository.save(vehicle_2);
            Driver driver_2 = new Driver("郭亮","421301891266",vehicle_2);
            driverRepository.save(driver_2);

            // 初始化测试车辆数据 - 赵宇
            Vehicle vehicle_3 = new Vehicle();
            vehicle_3.setLicensePlate("鄂 A957B9");
            vehicle_3.setModel("比亚迪汉");
            vehicle_3.setSeats(5);
            vehicle_3.setType(VehicleType.LUXURY);
            vehicle_3.setStatus(VehicleStatus.AVAILABLE);
            vehicleRepository.save(vehicle_3);
            Driver driver_3 = new Driver("赵宇", "421301891258", vehicle_3);
            driverRepository.save(driver_3);

            // 初始化测试车辆数据 - 孙悦
            Vehicle vehicle_4 = new Vehicle();
            vehicle_4.setLicensePlate("鄂 B835D7");
            vehicle_4.setModel("吉利帝豪");
            vehicle_4.setSeats(5);
            vehicle_4.setType(VehicleType.ECONOMY);
            vehicle_4.setStatus(VehicleStatus.AVAILABLE);
            vehicleRepository.save(vehicle_4);
            Driver driver_4 = new Driver("孙悦", "421301891259", vehicle_4);
            driverRepository.save(driver_4);

            // 初始化测试车辆数据 - 周伟
            Vehicle vehicle_5 = new Vehicle();
            vehicle_5.setLicensePlate("鄂 C672E9");
            vehicle_5.setModel("长安 CS75");
            vehicle_5.setSeats(5);
            vehicle_5.setType(VehicleType.ECONOMY);
            vehicle_5.setStatus(VehicleStatus.AVAILABLE);
            vehicleRepository.save(vehicle_5);
            Driver driver_5 = new Driver("周伟", "421301891260", vehicle_5);
            driverRepository.save(driver_5);

            // 初始化测试车辆数据 - 吴琳
            Vehicle vehicle_6 = new Vehicle();
            vehicle_6.setLicensePlate("鄂 D914F3");
            vehicle_6.setModel("红旗 H5");
            vehicle_6.setSeats(5);
            vehicle_6.setType(VehicleType.LUXURY);
            vehicle_6.setStatus(VehicleStatus.AVAILABLE);
            vehicle_6.setCompanyOwned(true);
            vehicleRepository.save(vehicle_6);
            Driver driver_6 = new Driver("吴琳", "421301891261", vehicle_6);
            driverRepository.save(driver_6);

            // 初始化测试车辆数据 - 郑浩
            Vehicle vehicle_7 = new Vehicle();
            vehicle_7.setLicensePlate("鄂 E586G2");
            vehicle_7.setModel("奇瑞瑞虎 8");
            vehicle_7.setSeats(5);
            vehicle_7.setType(VehicleType.ECONOMY);
            vehicle_7.setStatus(VehicleStatus.AVAILABLE);
            vehicle_7.setCompanyOwned(true);
            vehicleRepository.save(vehicle_7);
            Driver driver_7 = new Driver("郑浩", "421301891262", vehicle_7);
            driverRepository.save(driver_7);

            // 初始化测试车辆数据 - 钱峰
            Vehicle vehicle_8 = new Vehicle();
            vehicle_8.setLicensePlate("鄂 F741H8");
            vehicle_8.setModel("荣威 RX5");
            vehicle_8.setSeats(5);
            vehicle_8.setType(VehicleType.ECONOMY);
            vehicle_8.setStatus(VehicleStatus.AVAILABLE);
            vehicle_8.setCompanyOwned(true);
            vehicleRepository.save(vehicle_8);
            Driver driver_8 = new Driver("钱峰", "421301891263", vehicle_8);
            driverRepository.save(driver_8);

            // 初始化测试车辆数据 - 林晓
            Vehicle vehicle_9 = new Vehicle();
            vehicle_9.setLicensePlate("鄂 G369I5");
            vehicle_9.setModel("五菱宏光 S");
            vehicle_9.setSeats(7);
            vehicle_9.setType(VehicleType.ECONOMY);
            vehicle_9.setStatus(VehicleStatus.AVAILABLE);
            vehicle_9.setCompanyOwned(true);
            vehicleRepository.save(vehicle_9);
            Driver driver_9 = new Driver("林晓", "421301891264", vehicle_9);
            driverRepository.save(driver_9);

        }
        User admin=new User("root","123456","admin");
        userRepository.save(admin);
    }
}